package SecondeStage.IO.ObjectStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectInputStream_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 反序列化指定的文件
        String filePath ="D:\\Java\\JavaProject\\data.dat";
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(filePath));
        // 读取（反序列化）文件数据，顺序需要和保存数据（序列化）的顺序一致
        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readUTF());
        Object dog = ois.readObject();
        //dog 的编译类型是Object，运行类型是Dog
        System.out.println("运行类型=" +dog.getClass());
        System.out.println("dog信息="+dog);
        // 如果想要调用Dog的方法，需要向下转型
        Dog dog2 =(Dog)dog;
        System.out.println(dog2.getName());
        ois.close();
    }
}
